/**
 * 播种面板 - 单个果实
 */
class PlantItem extends ItemRendererBase {

    public constructor() {
        super();
        this.skinName = PlantItemSkin;
    }


    // 果实图片
    private imgPlant: eui.Image;
    // 果实名称
    private labName: eui.Label;
    // 果实价格
    private labPrice: eui.Label;
    // 果实数量
    private labHas: eui.Label;


    protected dataChanged() {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.ontap, this);

        this.imgPlant.source = "plant_" + this.data.id + "_png";
        this.labName.text = this.data.name;
        this.labPrice.text = this.data.price;
        this.labHas.text = this.data.i_have ? this.data.i_have : "0";
    }



    /**
     * 点击播种
     */
    private ontap() {
        // 判断是否有作物
        if (this.data.i_have < 1) {
            // 判断类型
            if (Number(this.data.id) === 1) {
                // 萝卜
                SceneManager.ins.confirm("没有作物啦!\分享到群可免费获得", e => {
                    if (e) {
                        MainMenu.ins.shareRepeat(5, s => {
                            if (s) {
                                // 请求加种子 还没有接口
                                Net.ins.send(['getLuobo'], {}, (e) => {
                                    // 分享成功
                                    SceneManager.ins.alert("分享成功，快播种吧")
                                    this.data.i_have = this.data.i_have ? this.data.i_have + 1 : 1;
                                    this.labHas.text = this.data.i_have;
                                })
                            }
                        })
                    }
                })
            } else if (Number(this.data.id) === 2) {
                // 猕猴桃 每日礼包
                SceneManager.ins.confirm("没有作物啦!\n前往每日礼包获取？", e => {
                    if (e) {
                        SceneManager.ins.popScene();
                        egret.setTimeout(function () {
                            SceneManager.ins.pushScene(new DaygiftPanel);
                        }, this, 300)
                    }
                })
            } else {
                // 去商城
                SceneManager.ins.confirm("没有作物啦!\n前往商城获取？", e => {
                    if (e) {
                        platform.skipExchange(5);
                    }
                })
            }
        } else {
            PlayerSelfData.landInfo((land_info) => {
                if (PlayerSelfData.tap_land_id == 0 && (typeof land_info[0]["plant_id"] != "undefined" && land_info[0]["plant_id"] != 0)) {
                    SceneManager.ins.alert("土地已经种满啦~~")
                    return;
                }
                Net.ins.send(['planting'], {
                    "land_id": PlayerSelfData.tap_land_id,
                    "plant_id": this.data.id
                }, (e) => {
                    SceneManager.ins.alert("播种成功")
                    SceneManager.ins.popScene();
                    this.data.i_have = this.data.i_have - 1;
                    CommonData.setplantInfo(this.data.id, this.data);
                    for (var i = 0; i < PlayerSelfData.landInfoLength; i++) {
                        if (land_info[i]["id"] == e.id) {
                            PlayerSelfData.setLandInfo(i, e)
                            EventManager.instance.dispatch(EventName.PLANT_PLAY_CHANGE, i);
                            break;
                        }
                    }
                });
            })
        }
    }

}